/**
 * Copyright &copy; 2016-2017 <a href="http://git.oschina.net/whoamien/backend_management">Backend Management</a> All rights reserved.
 */
package com.xw.app.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.CaseInsensitiveMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Maps;
import com.xw.app.dao.SysDictDAO;
import com.xw.app.model.SysDict;
import com.xw.app.service.SysDictService;
import com.xw.framework.base.service.BaseServiceImpl;

/**
 * The Class SysDictServiceImpl.
 *
 * @author Xi Wei
 */
@Service
@Transactional
public class SysDictServiceImpl extends BaseServiceImpl<SysDict, Long> implements SysDictService {

	@Autowired
	private SysDictDAO sysDictDAO;
	
	/* (non-Javadoc)
	 * @see com.xw.app.service.SysDictService#checkCodeExist(java.lang.String, java.lang.Long, java.lang.Long)
	 */
	@Override
	public boolean checkCodeExist(String dictCode,Long id,Long parentId){
		
		List<SysDict> list = null;
		if(id != null){
			if(parentId == null){
				list = sysDictDAO.findByDictCodeAndParentIdIsNullAndIdNot(dictCode,id);
			}else{
				list = sysDictDAO.findByDictCodeAndParentIdAndIdNot(dictCode,parentId,id);
			}
			
		}else{
			if(parentId == null){
				list = sysDictDAO.findByDictCodeAndParentIdIsNull(dictCode);
			}else{
				list = sysDictDAO.findByDictCodeAndParentId(dictCode, parentId);
			}
			
		}
		if(list == null || list.isEmpty()){
			return false;
		}
		return true;
	}
	
	
	/* (non-Javadoc)
	 * @see com.xw.app.service.SysDictService#getDictMapByCode(java.lang.String)
	 */
	@Override
	public Map<String, String> getDictMapByCode(String parentCode) {
		Map<String,String> map = new CaseInsensitiveMap();
		List<SysDict> list = sysDictDAO.findByParentCode(parentCode);
		list.forEach(x -> map.put(x.getDictCode(), x.getDictName()));
		return map;
	}
	
	/* (non-Javadoc)
	 * @see com.xw.app.service.SysDictService#getDictIntegerMapByCode(java.lang.String)
	 */
	@Override
	public Map<Integer,String> getDictIntegerMapByCode(String parentCode) {
		Map<Integer,String> map = Maps.newHashMap();
		List<SysDict> list = sysDictDAO.findByParentCode(parentCode);
		list.forEach(x -> map.put(Integer.valueOf(x.getDictCode()), x.getDictName()));
		return map;
	}
	
	/* (non-Javadoc)
	 * @see com.xw.app.service.SysDictService#getAllParentDict()
	 */
	@Override
	public List<SysDict> getAllParentDict(){
		return sysDictDAO.getAllParentDict();
	}

}
